package com.hoow.shakoow.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.hoow.shakoow.ProjectApplication
import com.hoow.shakoow.ProjectConfig
import com.hoow.shakoow.extend.*


/** 本地缓存工具 */
object SpUtil {

    //sp key token
    val SP_KEY_TOKEN = "TOKEN"

    //sp key user id
    val SP_KEY_USER_ID = "USER_ID"

    //sp key mobile
    val SP_KEY_MOBILE = "MOBILE"

    //sp key isAdSource
    val SP_KEY_IS_AD_SOURCE = "IS_AD_SOURCE"

    //sp key country
    val SP_KEY_COUNTRY = "COUNTRY"

    //sp key api path
    val SP_KEY_API_PATH = "APIPATH"

    /** 获取sp句柄 */
    fun getSp(): SharedPreferences {
        val sp = ProjectApplication.context.getSharedPreferences(
            ProjectConfig.Product,
            Context.MODE_PRIVATE
        )
        return sp
    }

    @SuppressLint("ApplySharedPref")
    fun saveSpValue(strKey: String, strValue: String) {
        val sp = getSp().edit()
        sp.putString(strKey, strValue)
        sp.commit()
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveToken(gToken: String) {
        saveSpValue(SP_KEY_TOKEN, gToken)
    }

    /** 获取 token */
    @SuppressLint("ApplySharedPref")
    fun getToken(): String {
        return getSp().getString(SP_KEY_TOKEN, "").toString()
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveUserId(gUserId: String) {
        saveSpValue(SP_KEY_USER_ID, gUserId)
    }

    /** 获取 user id */
    @SuppressLint("ApplySharedPref")
    fun getUserId(): String {
        return getSp().getString(SP_KEY_USER_ID, "")
            .toString()
    }

    /** 保存 mobile */
    @SuppressLint("ApplySharedPref")
    fun saveMobile(gMobile: String) {
        saveSpValue(SP_KEY_MOBILE, gMobile)
    }

    /** 获取 mobile */
    @SuppressLint("ApplySharedPref")
    fun getMobile(): String {
        return getSp().getString(SP_KEY_MOBILE, "")
            .toString()
    }

    /** 获取 isAdSource */
    @SuppressLint("ApplySharedPref")
    fun getIsAdSourceUser(): String {
        return getSp().getString(
            SP_KEY_IS_AD_SOURCE,
            "NO"
        )
            .toString()
    }

    /** 保存 isAdSource */
    @SuppressLint("ApplySharedPref")
    fun saveIsAdSource(gIsAdSource: String) {
        saveSpValue(
            SP_KEY_IS_AD_SOURCE,
            gIsAdSource
        )
    }

    /** 保存 country */
    @SuppressLint("ApplySharedPref")
    fun saveCountry(gCountry: String) {
        saveSpValue(
            SP_KEY_COUNTRY,
            gCountry
        )
    }

    /** 获取 country */
    @SuppressLint("ApplySharedPref")
    fun getCountry(): String {
        return getSp().getString(SP_KEY_COUNTRY, "")
            .toString()
    }

    /** 保存 api path */
    @SuppressLint("ApplySharedPref")
    fun saveApiPath(gApiPath: String) {
        saveSpValue(
            SP_KEY_API_PATH,
            gApiPath
        )
    }

    /** 获取 api path */
    @SuppressLint("ApplySharedPref")
    fun getApiPath(): String {
        return getSp().getString(SP_KEY_API_PATH, "")
            .toString()
    }
}